<?php
namespace app\index\controller;

use think\Controller;

class Product extends Common
{
    public function _initialize()
    {
        parent::_initialize();
        $this->assign('active', '产品中心');
        $url = input('param.cid', 0) > 0 ? '/p_list/' . input('param.cid', 0) : '/p_list';
        $this->assign('nav_image', get_nav_img($url));
    }

    // 产品列表页面
    public function index() {
        $cid = input('param.cid', 0);

        $this->assign('tree', get_category());

        $where = [];
        $cate_name = '全部产品';
        if($cid > 0){
            $where['cate_id'] = $cid;
            $cate_name = db('category')->where('id', $cid)->value('name');
        }
        $this->assign('cate_name', $cate_name);
        
        $product = db('product')->alias('p')
            ->field('p.name, p.id, p.is_index, pi.image')
            ->join('osl_product_image pi', 'p.id = pi.product_id and pi.is_main = 1', 'LEFT')
            ->where($where)
            ->order('p.id desc')
            ->paginate(9);

        $this->assign('product', $product);
        $this->assign('cid', $cid);
        return view();
    }

    // 产品详细页面
    public function product_details() {
        $this->assign('tree', get_category());

        $id = input('param.id');
        $product = db('product')->alias('p')
            ->field('p.name, p.cate_id, p.id, pi.image, pc.content')
            ->join('osl_product_image pi', 'p.id = pi.product_id and pi.is_main = 1', 'LEFT')
            ->join('osl_product_content pc', 'p.id = pc.product_id', 'LEFT')
            ->where('p.id', $id)
            ->find();
        //dump($product);
        $this->assign('product', $product);
        return view();
    }
}
